﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using NetDon.OA.Interface;
using NetDon.OA.Interface.CRM;
using NetDon.OA.Interface.eLogistics;
using NetDon.Utilities;

namespace NetDon.OA.Portal.eLogisticsQuery
{
    [Navigation("报表查询", "在途跟踪查询")]
    public partial class OnWayTrackQueryPage : PageBase
    {
        protected int? CurrentOrderStatus { get; set; }


        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {

                this.ddlCustomer.InsertEmptyItem(ListEmptyType.Select);
          

                txtReceivingOrderDateStart.Text =
 DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd");
                txtReceivingOrderDateEnd.Text =
              DateTime.Now.ToString("yyyy-MM-dd");

                ddlCompany.InsertEmptyItem(ListEmptyType.Select);

                if (!this.IsSuperRoles())
                {
                    ddlCompany.SelectedValue = CurrentApplicationContext.CompanyId.ToString();
                    ddlCompany.Enabled = false;
                }
            }
        }



        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            ObjectDataSourceCustomers.Selecting += (ds, dsArgs) =>
            {
                dsArgs.InputParameters["areaId"] = CurrentApplicationContext.CompanyId.ToString();
            };


        }

        public void ddlCustomer_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(ddlCustomer.SelectedValue))
            {

                LoadGoodsShippedTypes(ddlCustomer.SelectedValue.ToGuid());
            }


        }

        private void LoadGoodsShippedTypes(Guid customerId)
        {
            this.ddlGoodsShippedTypeId.Items.Clear();
            this.ddlGoodsShippedTypeId.DataSource = ServiceContainer.GetService<ICustomerInfoService>().GetCustomerGoodsShippTypes(
               customerId, CurrentApplicationContext.CompanyId);
            this.ddlGoodsShippedTypeId.DataBind();
            ddlGoodsShippedTypeId.InsertEmptyItem(ListEmptyType.Select);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Session["OnWayTrackQueryPage"] = GetSearchCriteria();
            this.ScriptStartup("search();");

        }

        protected IssuingOrderOnWayTrackSearchCriteria GetSearchCriteria()
        {
            var criteria = new IssuingOrderOnWayTrackSearchCriteria
            {

            };
            if (!MiscUtilities.IsNullOrEmpty(ddlCompany.SelectedValue))
            {
                criteria.AreaId = ddlCompany.SelectedValue.ToGuid();
            }
            DateTime? dateStart = null, dateEnd = null;
            if (!string.IsNullOrEmpty(txtReceivingOrderDateStart.Text.Trim()))
            {
                dateStart = DateTime.Parse(txtReceivingOrderDateStart.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtReceivingOrderDateEnd.Text.Trim()))
            {
                dateEnd = DateTime.Parse(txtReceivingOrderDateEnd.Text.Trim());
            }
            if (dateStart.HasValue || dateEnd.HasValue)
            {
                criteria.ReceivingOrderDate = new Range<DateTime?>(dateStart, dateEnd);
            }

            dateStart = null; dateEnd = null;
            if (!string.IsNullOrEmpty(txtGoodsDeliveredDateStart.Text.Trim()))
            {
                dateStart = DateTime.Parse(txtGoodsDeliveredDateStart.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtGoodsDeliveredDateEnd.Text.Trim()))
            {
                dateEnd = DateTime.Parse(txtGoodsDeliveredDateEnd.Text.Trim());
            }
            if (dateStart.HasValue || dateEnd.HasValue)
            {
                criteria.PlannedGoodsDeliveredDate = new Range<DateTime?>(dateStart, dateEnd);
            }
            dateStart = null; dateEnd = null;
            if (!string.IsNullOrEmpty(txtActualDeliveredDateStart.Text.Trim()))
            {
                dateStart = DateTime.Parse(txtActualDeliveredDateStart.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtActualDeliveredDateEnd.Text.Trim()))
            {
                dateEnd = DateTime.Parse(txtActualDeliveredDateEnd.Text.Trim());
            }
            if (dateStart.HasValue || dateEnd.HasValue)
            {
                criteria.ActualDeliveredDate = new Range<DateTime?>(dateStart, dateEnd);
            }
            if (!string.IsNullOrEmpty(txtTransportationPlanCode.Text))
            {
                criteria.TransportationPlanCode = txtTransportationPlanCode.Text.Trim();
            }
            if (!string.IsNullOrEmpty(ddlCity.SelectedValue))
            {
                criteria.DeliveryCity = ddlCity.SelectedValue;
            }
            if (!string.IsNullOrEmpty(txtTransportationPlanCode.Text))
            {
                criteria.TransportationPlanCode = txtTransportationPlanCode.Text.Trim();
            }

            if (!MiscUtilities.IsNullOrEmpty(ddlRouteTypeId.SelectedValue.ToGuid()))
            {
                criteria.RouteTypeId = ddlRouteTypeId.SelectedValue.ToGuid();
            }
            if (!string.IsNullOrEmpty(ddlGoodsShippedTypeId.SelectedValue))
            {
                criteria.GoodsShippedTypeId = ddlGoodsShippedTypeId.SelectedValue.ToGuid();
            }
            if (!MiscUtilities.IsNullOrEmpty(ddlCustomer.SelectedValue.ToGuid()))
            {
                criteria.CustomerId = ddlCustomer.SelectedValue.ToGuid();
            }
        
            if (ckbIsGoodsDelayedNo.Checked || ckbIsGoodsDelayedYes.Checked)
            {
                criteria.IsGoodsDelayed = ckbIsGoodsDelayedYes.Checked;
            }
            if (ckbIsGoodsExceptionalNo.Checked || ckbIsGoodsExceptionalYes.Checked)
            {
                criteria.IsGoodsExceptional = ckbIsGoodsExceptionalYes.Checked;
            }
            if (!string.IsNullOrWhiteSpace(ddlIssuingOrderStatus.SelectedValue))
            {
                criteria.OrderStatus = (int)ddlIssuingOrderStatus.SelectedValue.ToEnum<OrderStatus>();
            }
            return criteria;
        }









    }
}